<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>油卡列表</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
	<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
	<meta http-equiv="refresh" content="60">
</head>
<body>
<div class="ok-body">
	<!--模糊搜索区域-->
	<div class="layui-row">
		<form class="layui-form ok-search-form">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">原价金额</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" placeholder="原价金额" autocomplete="off" name="dmoney">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">手机号</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" placeholder="手机号" autocomplete="off" name="dphone">
					</div>
				</div>
				<div class="layui-inline">
					<div class="layui-input-inline">
						<button class="layui-btn" lay-submit=""  lay-filter="search">搜索
							<i class="layui-icon">&#xe615;</i>
						</button>
					</div>
				</div>
			</div>
		</form>
	</div>
	<br>
	<!--数据表格-->
	<!-- <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table> -->
	<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
	layui.use(["element", "table", "form", "laydate", "okLayer", "okUtils", "okMock","jquery"], function () {
		let table = layui.table;
		let form = layui.form;
		let laydate = layui.laydate;
		let okLayer = layui.okLayer;
		let okUtils = layui.okUtils;
		let okMock = layui.okMock;
		var $ = layui.$;
		okLoading.close();

		// laydate.render({elem: "#startTime", type: "datetime"});
		// laydate.render({elem: "#endTime", type: "datetime"});

		table.render({
			elem: "#tableId",
			url: "/vip/sel",
			limit: 10,
			page: true,
			toolbar: "#toolbarTpl",
			size: "sm",
			cols: [[
				{type: "checkbox"},
				{field: "id", title: "ID",width: 70,align: "center"},
				{field: "goodsname", title: "goodsname",width: 120,align: "center"},
				{field: "orderno", title: "订单编号",width: 200,align: "center"},
				{field: "outorderno", title: "微信订单号",width: 200,align: "center"},
				{field: "num", title: "购买数量",width: 80,align: "center"},
				{field: "paymoney", title: "付款金额",width: 90,align: "center"},
				// {field: "goodsprice", title: "商品价格",width: 160,align: "center"},
				{field: "money", title: "订单原价",width: 160,align: "center"},
				{field: "wxphone", title: "微信手机号",width: 130,align: "center"},
				{field: "desc", title: "退款原因",width:100,align: "center"},
				// {field: "type", title: "是否发货成功",width: 100,align: "center"},
				{field: "type",
					templet: function(d) {
						if(d.type == '发货成功') {
							return ' <span style="color: #d8f624;">' + d.type + '</span>'
						} else if(d.type == '发货失败') {
							return ' <span style="color: #d70909;">' + d.type + '</span>'
						} else if(d.type == '已退款') {
							return ' <span style="color: #ff0099;">' + d.type + '</span>'
						} else if(d.type == '进行中') {
							return ' <span style="color: #cc0099;">' + d.type + '</span>'
						} else if(d.type == '已撤回') {
							return ' <span style="color: #9933ff;">' + d.status + '</span>'
						} else {
							return ' <span style="color: #06fa58;">' + d.status + '</span>';
						}
					},
					title: "充值状态",width: 110,align: "center"},
				{title: "操作", width: 120, align: "left", templet: "#operationTpl"}
			]],

			done: function (res, curr, count) {
			}

		});
		form.on("submit(search)", function (data) {
			var result = data.field;
			table.reload('tableId',{
				// where: data.field,
				// page: {curr: curr},
				page: {
					curr: 1
				}
				, where: {
					outorderno: result.outorderno,
					orderno: result.orderno,
				}
			}, 'data');

			return false;
		});

		table.on("toolbar(tableFilter)", function (obj) {
			switch (obj.event) {
				case "batchDel":
					batchDel();
					break;
			}
		});
		table.on("tool(tableFilter)", function (obj) {
			let data = obj.data;
			switch (obj.event) {
				case "cupd":
					cupd(data.id);
					break;
				case "tupd":
					tupd(data);
					break;
				case "dupd":
					dupd(data.id);
					break;
			}
		});
		function batchDel() {
			okLayer.confirm("确定要批量删除吗？", function (index) {
				layer.close(index);
				let idsStr = okUtils.tableBatchCheck(table);
				console.log(idsStr);
				if (idsStr) {
					okUtils.ajax("/vip/dupds", "post", {idsStr: idsStr,}, true).done(function (response) {
						okUtils.tableSuccessMsg(response.msg);
						table.reload();
						return false;
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}
		function cupd(id) {
			okLayer.confirm("确定对用户进行充值吗？", function () {
				console.log(id)
				okUtils.ajax("/vip/cupd", "post", {id: id}, true).done(function (response) {
					okUtils.tableSuccessMsg(response.msg);
					table.reload();
					return false;
				}).fail(function (error) {
					console.log(error)
				});
			})
		}
		function formatMoneyYuanToFen(yuan,digit){

			var m=0,

					s1=yuan.toString(),

					s2=digit.toString();

			try{m+=s1.split(".")[1].length}catch(e){}

			try{m+=s2.split(".")[1].length}catch(e){}

			return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)

		}
		function tupd(data) {
			okLayer.confirm("确定要退款吗？", function () {
				console.log(data.outorderno)
				// console.log(data.fmoney)
				// console.log("id"+data.id+",money:"+formatMoneyYuanToFen(data.paymoney,100),"desc")
				$.ajax({url:"http://localhost:8080/refund",type:"POST",dataType:'xml',data:{orderNum:data.outorderno,money:formatMoneyYuanToFen(data.paymoney,100),desc:''},success:function (res){
						console.log(res);
						var return_code = $("return_code", $(res)).text();
						var return_msg = $("return_msg", $(res)).text();
						if (return_code=="SUCCESS"&&return_msg=="OK"){
							okUtils.ajax("/vip/tupd", "post", {id: data.id}, true).done(function (response) {
								okUtils.tableSuccessMsg(response.msg);
								table.reload();
								return false;
							}).fail(function (error) {
								okUtils.tableSuccessMsg(response.msg);
								table.reload();
								return false;
							});
						}
					}}
				)

			})
		}
		function dupd(id) {
			okLayer.confirm("确定对用户进行删除吗？", function () {
				okUtils.ajax("/vip/dupd", "post", {id: id}, true).done(function (response) {
					okUtils.tableSuccessMsg(response.msg);
					table.reload();
					return false;
				}).fail(function (error) {
					console.log(error)
				});
			})
		}
	})
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
	<div class="layui-btn-container">
		<div class="layui-inline" lay-event="batchDel"><i class="layui-icon layui-icon-delete"></i></div>
	</div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">


	{{#  if(d.type == '发货失败'){ }}
<!--	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="cupd">充值</a>-->
	<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="tupd">退款</a>
	{{#  } else if(d.type == '发货成功') { }}
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="dupd">删除</a>
	{{#  } else if(d.type == '已退款') { }}
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="dupd">删除</a>
	{{#  } }}
</script>
</body>
</html>
